/**
 * 建立 唯一 臨時的 style 標簽 到 head
 * @param {String} id -- 設置一個挂載到 window.StaticCss[$1] 的唯一標識
 */
function SetStaticCssTxtFunc(id) {
  window.StaticCss = window.StaticCss
    ? (function () {
        let a = window.StaticCss;
        a['id'].indexOf(id) == -1
          ? (function () {
              a['id'].push(id);
              a[id] = {
                classArr: [],
              };
            })()
          : undefined;
        return a;
      })()
    : (function () {
        let a = {};
        a['id'] = [id];
        a[id] = {
          classArr: [],
        };
        return a;
      })();
  let totalStaticCss = window.StaticCss;

  /**
   *
   * @param {String} cla  -- accurate id String ！！
   * @param {String} code -- Packaged css animation String ！！
   */
  function avanda(cla, code) {
    // if (cla == undefined && code == undefined) {
    if (arguments.length == 0 && arguments[0] == undefined) {
      //返回 縂的 已經定義的 元素
      return claArr;
    }
    // console.log(claArr);
    if (cla == undefined) {
      console.log(
        'sorry we need a id <String> to manage the variable \n plase enter a arguments[0] value'
      );
      return null;
    }

    let hasClaCss = false;
    // console.log("沒進來 ");
    let claArr = totalStaticCss[id];
    // console.log(claArr);

    claArr['classArr'].indexOf(cla) == -1
      ? (function () {
          // console.log("添加")
          claArr['classArr'].push(cla);
          claArr[cla] = code;
        })()
      : (function () {
          hasClaCss = true;
          // console.log("添加過了")

          //cover style code
          let targetStyle = document.getElementById(cla);
          try {
            //for Chrome Firefox Opera Safari
            targetStyle.appendChild(document.createTextNode(code));
          } catch (ex) {
            //for IE
            targetStyle.styleSheet.cssText = code;
          }
        })();

    if (hasClaCss == true) {
      // console.log("you have settled \"" + cla + "\" id CssStyle Now!  In head tag");
      return true;
    }
    if (code != undefined) {
      var style = document.createElement('style');
      //   style.type = 'text/css';
      style.rel = 'stylesheet';
      try {
        //for Chrome Firefox Opera Safari
        style.appendChild(document.createTextNode(code));
      } catch (ex) {
        //for IE
        style.styleSheet.cssText = code;
      }
      style.setAttribute('class', `static_${cla}`);
      style.setAttribute('id', `${cla}`);

      var head = document.getElementsByTagName('head')[0];
      head.appendChild(style);
    } else if (code == 'remove') {
      if (claArr['classArr'].indexOf(cla) != -1) {
        claArr['classArr'].splice(claArr['classArr'].indexOf(cla), 1);
        // console.log(claArr);
        var claEle = document.getElementById(`${cla}`);
        claEle.remove();
        return `${cla} has been removed`;
      }
      return `${cla} is undefined`;
    } else if (code == undefined) {
      if (claArr['classArr'].indexOf(cla) != -1) {
        //返回 指定的 class
        return claArr['classArr'][cla];
      }
      return `${cla} is undefined`;
    }
  }

  return avanda;
}

export { SetStaticCssTxtFunc };
